package cn.edu.experiment2;

import java.util.ArrayList;
import java.util.List;

public class Student {
    private String id;
    private String name;
    private List<Course> courses = new ArrayList<>();   // ←改成列表

    public Student(String id, String name) {
        this.id = id;
        this.name = name;
    }

    /* 选课：保持原来打印文案 */
    public void select(Course c) {
        if (c == null) return;
        for (Course course : courses) {                 // 已选不再重复
            if (course.getId().equals(c.getId())) {
                System.out.println("学生 " + name + " 已选过 " + c.getName() + "，忽略");
                return;
            }
        }
        courses.add(c);
        System.out.println("学生 " + name + " 选课：" + c.getName());
    }

    /* 退课：退掉最早选的一门（保持原“退课”打印）*/
    public void drop() {
        if (courses.isEmpty()) return;
        Course c = courses.remove(0);
        System.out.println("学生 " + name + " 退课：" + c.getName());
    }

    /* 课表：保持原来格式，一个学生一块 */
    public void printSchedule() {
        System.out.println("========== 学生个人课表 ==========");
        System.out.println("编号：" + id);
        System.out.println("课程名称：" + (courses.isEmpty() ? "暂无" :
                courses.stream().map(Course::getName).reduce((a,b)->a+"，"+b).orElse("暂无")));
        System.out.println("上课地点：" + (courses.isEmpty() ? "暂无" :
                courses.stream().map(Course::getLocation).reduce((a,b)->a+"，"+b).orElse("暂无")));
        System.out.println("时间：" + (courses.isEmpty() ? "暂无" :
                courses.stream().map(Course::getTime).reduce((a,b)->a+"，"+b).orElse("暂无")));
        System.out.println("授课教师：" + (courses.isEmpty() ? "暂无" :
                courses.stream().map(c->c.getTeacher()==null?"待定":c.getTeacher().getName())
                        .reduce((a,b)->a+"，"+b).orElse("暂无")));
    }
}